//
//  AdsView.m
//  QuickCure
//
//  Created by Kent on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "AdsView.h"
#import "NetWorkTool.h"
#import <UIImageView+WebCache.h>

@interface AdsView () <UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView *scrollView;

@property (nonatomic,weak) UIPageControl *pageControl;

@property (nonatomic,strong) NSTimer *timer;

@end

@implementation AdsView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {

        UIScrollView *scrollView = [[UIScrollView alloc]init];
        scrollView.delegate = self;
        self.scrollView = scrollView;
        [self addSubview:scrollView];
        
        [scrollView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self generateContent];
    }
    return self;
}

- (void)generateContent {
    
    NSArray *imageArray = @[
                            @"http://img.guahao.com/portal_upload/rollingnews/1453447883150.jpg",
                            @"http://img.guahao.com/portal_upload/mediafocus/1461826474435.jpg",
                            @"http://img.guahao.com/portal_upload/rollingnews/1461574027543.jpg"
                            ];
    
    UIImageView *lastImgView;
    
    for (int i = 0; i < imageArray.count; i++) {
        
        UIImageView *svImgView = [[UIImageView alloc]init];
        [svImgView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]]];
        [self.scrollView addSubview:svImgView];
        
        [svImgView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastImgView ? lastImgView.right : @0);
            make.top.equalTo(self.scrollView);
            make.width.equalTo(sWidth);
            make.height.equalTo(self);
        }];
        
        lastImgView = svImgView;
    }
    
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastImgView.right);
    }];
    
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = imageArray.count;
    pageControl.currentPage = 0;
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    self.pageControl = pageControl;
    [self addSubview:pageControl];
    
    [pageControl makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self);
        make.height.equalTo(30);
    }];
    
    [self startTimer];
    
}

-(void)removeTimer {
    [self.timer invalidate];
    self.timer = nil;
}

-(void)startTimer {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    self.timer = timer;
    
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)autoScroll {

    UIView *view = [self.scrollView.subviews lastObject];
    
    CGFloat lastX = CGRectGetMaxX(view.frame) - self.scrollView.bounds.size.width;
    
    if (self.scrollView.contentOffset.x >=  lastX ) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }else{
        
        CGPoint offset = self.scrollView.contentOffset;
        offset.x += self.scrollView.bounds.size.width;

        [self.scrollView setContentOffset:offset animated:YES];
        
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat scrollWidth = scrollView.bounds.size.width;
    
    NSInteger pageNum = (scrollView.contentOffset.x + scrollWidth * 0.5) / scrollWidth;
    self.pageControl.currentPage = pageNum;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self startTimer];
}



@end











































